package com.abbcc.module.admin;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.abbcc.action.BaseAction;
import com.abbcc.common.CommonConst;
import com.abbcc.models.TdData;
import com.abbcc.service.DataService;
import com.abbcc.util.StringUtil;

public class DataAction extends BaseAction<TdData> {
	@Autowired
	private DataService dataService;
	private int belongPageId;
	private String belongPageName;

	public String list() {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if (entity.getBelongId() != 0) {
			dc.add(Restrictions.eq("belongId", entity.getBelongId()));
			belongPageId = entity.getBelongId();
			TdData td = new TdData();
			dataService.load(td, belongPageId);
			belongPageName = td.getName() + "的子类";

		} else {
			dc.add(Restrictions.eq("isRoot", "0"));
		}
		dc.addOrder(Order.desc("dataId"));
		pageList = dataService.findPageByCriteria(dc, pageSize, startIndex);
		if(entity.getBelongId()==0 && pageList.getTotalCount()==0)
			initData();
		saveLog("数据配置", "", CommonConst.LOGFIND, CommonConst.LOGTYPEBYADMIN);
		return LIST;
	}

	public String save() {
		if(StringUtil.isBlank(entity.getName())){
			result = "添加失败,标识或名称不能为空!";
			return list();
		}
		if(!checkAlias()){
			result = "添加失败!该标识已经存在。";
			return list();
		}
			
		if (belongPageId!=0)
			entity.setIsRoot("1");
		else
			entity.setIsRoot("0");
		entity.setName(entity.getName().trim());
		dataService.save(entity);
		if (belongPageId!=0){
			removeSessionByDataId(entity.getBelongId());
		}
		result = "添加成功!";
		saveLog("数据配置-->栏目:" + entity.getName(), result, CommonConst.LOGSAVE,
				CommonConst.LOGTYPEBYADMIN);
		return list();
	}
	private void removeSessionByDataId(int bId){
		TdData td = (TdData) dataService.findById(entityClass, bId);
		if(td!=null)
			getSession().removeAttribute(td.getAlias()+"List");
	}
	private boolean checkAlias(){
		TdData td = new TdData();
		td.setAlias(entity.getAlias());
		td.setIsRoot("0");
		return dataService.findByExample(td).isEmpty();
	}
	public String del() {
		TdData td = new TdData();
		td.setBelongId(entity.getDataId());
		if (!dataService.findByExample(td).isEmpty()) {
			result = "删除失败，该栏目有下属栏目，请先删除其下属栏目!";
			return list();
		}
		dataService.delete(getEntityById());
		result = "删除成功!";
		saveLog("数据配置-->栏目:" + td.getName(), result, CommonConst.LOGDEL,
				CommonConst.LOGTYPEBYADMIN);
		return list();
	}

	public String updateName() {
		String newName = entity.getName();
		TdData td = getEntityById();
		String oldName = td.getName();
		td.setName(newName);
		dataService.update(td);
		result = "修改成功!";
		saveLog("数据配置-->名称:" + oldName, "修改后的名称为:" + newName + "," + result,
				CommonConst.LOGUPDATE, CommonConst.LOGTYPEBYADMIN);
		return JSON;
	}
//初始化数据
	private String initData() {
		String[] names = { "name", "prodType", "hinge", "handle", "locks",
				"catseye", "doorbell", "color", "doorframe", "threshold",
				"transom", "sdoor", "sframe","direction","pace"};
		String[] n = { "名称", "门面款式", "铰链", "拉手", "锁具", "猫眼", "门铃", "颜色", "门框",
				"门槛", "气窗", "小门", "小窗","开门方向","进度" };
		for(int i=0;i<names.length;i++){
			TdData td = new TdData();
			td.setAlias(names[i]);
			td.setName(n[i]);
			td.setIsRoot("0");
			dataService.save(td);
			if(names[i].equals("name")){
				TdData tttt = new TdData();
				tttt.setAlias(names[i]);
				tttt.setIsRoot("0");
				int tId = dataService.findByExample(tttt).get(0).getDataId();
				String[] nn = {"四开子母封闭","四开封闭","对开封闭","子母封闭","单门封闭","单门","子母","对开","四开","四开子母"};
				String[] nns = {"skzmfb","skfb","dkfb","zmfb","dmfb","dm","zm","dk","sk","skzm"};
				for(int j=0;j<nn.length;j++){
					TdData td1 = new TdData();
					td1.setAlias(nns[j]);
					td1.setName(nn[j]);
					td1.setIsRoot("1");
					td1.setBelongId(tId);
					dataService.save(td1);
				}
			}
			if(names[i].equals("doorframe")){
				TdData tttt = new TdData();
				tttt.setAlias(names[i]);
				tttt.setIsRoot("0");
				int tId = dataService.findByExample(tttt).get(0).getDataId();
				TdData t = new TdData();
				t.setAlias("bk");
				t.setName("边框");
				t.setBelongId(tId);
				t.setIsRoot("1");
				dataService.save(t);
				TdData t1 = new TdData();
				t1.setAlias("wbk");
				t1.setName("外边框");
				t1.setBelongId(tId);
				t1.setIsRoot("1");
				dataService.save(t1);
			}
		}
		
		return list();
	}

	private TdData getEntityById() {
		return (TdData) baseService.findById(entityClass, entity.getDataId());
	}

	public int getBelongPageId() {
		return belongPageId;
	}

	public void setBelongPageId(int belongPageId) {
		this.belongPageId = belongPageId;
	}

	public String getBelongPageName() {
		return belongPageName;
	}

	public void setBelongPageName(String belongPageName) {
		this.belongPageName = belongPageName;
	}

}
